# Maintainer: Swarnava Ghosh <swarnavaghosh04@gmail.com>
# Credits: Maximilian Schiller <manimax3@outlook.de> [https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=krpc-cpp]

_realname=krpc-cpp
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=0.4.9
_kspver=1.12.1
pkgrel=2
pkgdesc="KRPC Client for C++"
arch=('any')
url="https://github.com/krpc/krpc"
license=('LGPL3')
depends=(
  "${MINGW_PACKAGE_PREFIX}-asio"
  "${MINGW_PACKAGE_PREFIX}-protobuf")
makedepends=(
  "unzip"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://github.com/nullprofile/krpc/releases/download/$pkgver-$_kspver/krpc-$pkgver-$_kspver.zip"
        "001-krpc-cpp-cmake.patch")
sha256sums=('d86eb6ec6dc250b5a6ef9d4c5fd2c2d8180e1825320ba9f79c0b314e4f9a0699'
            '77e6028756bcfd06aa7ca6265327f9cf43ccade1ebcc657e826fea85d12aa0fd')
noextract=("krpc-$pkgver-$_kspver.zip")

prepare() {
  unzip krpc-$pkgver-$_kspver.zip client/$_realname-$pkgver.zip
  unzip client/$_realname-$pkgver.zip
  rm -rf client *.zip

  cd "${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}"/001-$_realname-cmake.patch
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .
}
